from django.urls import reverse
from django.http import QueryDict


def memory_url(request,name,*args,**kwargs):
    """
    生成带有源搜索条件的url
    :param request:
    :param name:
    :return:
    """
    basic_url = reverse(name,args=args,kwargs=kwargs)
    if not request.GET:
        # 当前url中无参数
        return basic_url
    query_dict = QueryDict(mutable=True)
    query_dict['_filter'] = request.GET.urlencode()
    return "%s?%s" % (basic_url,query_dict.urlencode())



def memory_reverse(request, name, *args, **kwargs):
    """
    反向生成URL:
            http://127.0.0.1:8000/rbac/menu/edit/1?_filter=mid%3D1
            1、将url原来搜索的条件，如 filter后边的值
            2、reverse生成原来的url，如 menu/edit
            3 将搜索条件和url进行

    :param request:
    :param name:
    :param args:
    :param kwargs:
    :return:
    """
    url = reverse(name, args=args, kwargs=kwargs)
    origin_parms = request.GET.get('_filter')
    if origin_parms:
        url = "%s?%s" % (url, origin_parms)
    return url
